package com.zml.control;

import com.alibaba.fastjson.JSON;
import com.huawei.ailifeability.netconfig.api.NetConfigApi;
import com.huawei.ailifeability.netconfig.model.CommonInfo;

import com.zml.control.entity.OcMqttEntity;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.eventhandler.EventHandler;  //线程间通信
import ohos.eventhandler.EventRunner;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * NetworkDeviceDataHandler is used to control Harmony devices. An example of a fan is provided as a reference.
 * Implement two methods, recoverData is used to initialize data, modifyProperty is used to respond to interface
 * operations. {@link NetworkDeviceDataHandler#sendMessage} is used to send messages to the device, After
 * sending successfully, {@link NetworkDeviceDataHandler#onDeviceDataChange(Map)} is called to refresh the UI interface
 */
public class NetworkDeviceDataHandler extends BaseDeviceDataHandler {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.DEBUG, 0, "MessageDataHandler");
    private static String sessionId = null;
    private final DeviceDataModel dataModel;
    private final EventHandler mainHandler;
    private final Map<String, Object> dataMap = new HashMap<>();
    private final DeviceDataModel solarDataModel = new DeviceDataModel() {
        @Override
        public Map<String, Object> recoverData() {
//            dataMap.put("switch/on", 0);
//            dataMap.put("windMode/mode", 0);
//            dataMap.put("windGear/gear", 0);
//            dataMap.put("shutdown/mode", 0);
            dataMap.put("infoDisplay/battery", 55);    //add
//            dataMap.put("bluetoothConnectStatus/bluetoothConnectStatus", 1);    //add
            System.out.println("NetworkDeviceDataHandler：entry");

            // 获取华为云IotDA设备影子数据
            String url = "https://iotda.cn-north-4.myhuaweicloud.com/v5/iot/0c2785b9a98010d52f9dc00c70d11445/devices/62687d6cb9f9fa4a4f9e3f17_OHOS_MPPT002/shadow"; //TODO：
            Map<String, String> paramsMap = new HashMap<String,String>();       //数据采用的哈希表结构
            Map<String, String> headerMap = new HashMap<String,String>();       //数据采用的哈希表结构
            // [{"key":"Content-Type","value":"application/json","description":"","type":"text","enabled":true}]
            headerMap.put("Content-Type", "application/json");//给map中添加元素
            headerMap.put("X-Auth-Token", "MIIWTQYJKoZIhvcNAQcCoIIWPjCCFjoCAQExDTALBglghkgBZQMEAgEwghRfBgkqhkiG9w0BBwGgghRQBIIUTHsidG9rZW4iOnsiZXhwaXJlc19hdCI6IjIwMjItMDctMDVUMjI6NDA6MjMuNDE1MDAwWiIsIm1ldGhvZHMiOlsicGFzc3dvcmQiXSwiY2F0YWxvZyI6W10sInJvbGVzIjpbeyJuYW1lIjoidGVfYWRtaW4iLCJpZCI6IjAifSx7Im5hbWUiOiJ0ZV9hZ2VuY3kiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9lY3Nfc3BvdF9pbnN0YW5jZSIsImlkIjoiMCJ9LHsibmFtZSI6Im9wX2dhdGVkX2l2YXNfdmNyX3ZjYSIsImlkIjoiMCJ9LHsibmFtZSI6Im9wX2dhdGVkX2FfY24tc291dGgtNGMiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9lY3Nfa2FlMSIsImlkIjoiMCJ9LHsibmFtZSI6Im9wX2dhdGVkX2R3c19wb2MiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9jYnJfZmlsZSIsImlkIjoiMCJ9LHsibmFtZSI6Im9wX2dhdGVkX2Vjc19rYzFfdXNlcl9kZWZpbmVkIiwiaWQiOiIwIn0seyJuYW1lIjoib3BfZ2F0ZWRfbWVldGluZ19lbmRwb2ludF9idXkiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9tYXBfbmxwIiwiaWQiOiIwIn0seyJuYW1lIjoib3BfZ2F0ZWRfZWdfY24iLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9zaXNfc2Fzcl9lbiIsImlkIjoiMCJ9LHsibmFtZSI6Im9wX2dhdGVkX3NhZF9iZXRhIiwiaWQiOiIwIn0seyJuYW1lIjo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QYJKoZIhvcNAQEBBQAEggEAEjsKdf6E4r6JrkONUHF0AXIRCI95JMlgaOtz-WT3iG9UkrhRsZy6w5HPuR2erRhfg-P+dFM-rFCGIdri0h1Cs0dNvmNDgpP1gRpsLo+PZPZqlaHTYmn5WgdnwfUHWtg8ES6DteygjlnDGur-xvf3b2KbQT2ZOQcAc3ls6wHylUVBCX8u7iip1wjbTi0WrPyzV+kz-vDWJ7qfs8PSCxhI1As9z5-j+hh-taS0tM1DrtYV08gx4gUd5dDvphg22r48SfhyqVCr5AjaLKBBtNLdZy8Xc6rNIVLaU4TiNWuXdMAr3-Gpc-wUhl8xSV9W4BPaZiGMpEp1fiyRO9mnZdiP1Q==");//给map中添加元素

//            Map<String, Object> dataMap = new HashMap<>();  // 将get到的数据保存在dataMap
            // 发起get请求
            ZZRHttp.get(url,paramsMap,headerMap,new ZZRCallBack.CallBackString() {
                @Override
                public void onFailure(int i, String s) {
                    System.out.println("API返回失败");
                    HiLog.info(LABEL_LOG, "API返回失败");
                }
                @Override
                public void onResponse(String s) {
                    System.out.println("API返回成功，结果："+s);
                    HiLog.info(LABEL_LOG, "API返回成功，结果："+s);
                    try {
                        //1.调用fastjson解析，结果保存在JSON对应的类
                        OcMqttEntity ocmqttentity = JSON.parseObject(s,OcMqttEntity.class);

                        //这部分用来更新UI
                        ZSONObject zsonObject = new ZSONObject(); //首先，将要刷新的数据存放在一个ZSONObject实例中
                        //失败返回{"error_msg":"Incorrect IAM authentication information: x-auth-token not found","error_code":"APIG.0301","request_id":"483907208e6ce86b662ee9571d65f113"}
                        if(ocmqttentity.getDevice_id() != null){
                            float InVoltage = ocmqttentity.getShadow().get(0).getReported().getProperties().getInVoltage()/1000;
                            float OutVoltage = ocmqttentity.getShadow().get(0).getReported().getProperties().getOutVoltage()/1000;
                            float InCurrent = ocmqttentity.getShadow().get(0).getReported().getProperties().getInCurrent();
                            float InPower = OutVoltage*InCurrent;
                            float efficiency = 98;

//                            zsonObject.put("service_id",ocmqttentity.getDevice_id());
                            zsonObject.put("InVoltage",0);
                            zsonObject.put("OutVoltage",0);
                            zsonObject.put("InCurrent",0);
                            zsonObject.put("InPower",0);
                            zsonObject.put("efficiency",0);

                        }else {
                            System.out.println("NetworkDeviceDataHandler：result");
                            HiLog.info(LABEL_LOG, "result.toString(): ");  // test
                        }
                        for (Map.Entry<String, Object> entry : zsonObject.entrySet()) {
                            dataMap.put(entry.getKey(), entry.getValue());
                            System.out.println("NetworkDeviceDataHandler：entry.getKey()"+entry.getKey());
                            System.out.println("NetworkDeviceDataHandler：entry.getValue()"+entry.getValue().toString());
                        }
                    } catch (Exception e) {
                        System.out.println("解析失败"+ e.getMessage());
                        HiLog.info(LABEL_LOG, "解析失败"+ e.getMessage());
                    }
                    // 更新UI
                    mainHandler.postTask(() -> initProfileData(0, "", dataMap));
                }
            });

            return dataMap;
        }

        @Override
        public Map<String, Object> modifyProperty(String key, Object value) {
            HashMap<String, Object> tmpMap = new HashMap<>();
            tmpMap.put(key, value);
            switch (key) {
                case "switch/on":
                    if ((Integer) value == 0) {
                        sendMessage("010000", tmpMap);
                        tmpMap.put("shutdown/mode", 0);
                    } else {
                        sendMessage("010100", tmpMap);
                    }
                    break;
                case "windMode/mode":
                    if ((Integer) value == 2) {
                        sendMessage("030300", tmpMap);
                    } else {
                        sendMessage("030100", tmpMap);
                    }
                    break;
                case "windGear/gear":
                    if ((Integer) value == 2) {
                        sendMessage("020100", tmpMap);
                    } else if ((Integer) value == 3) {
                        sendMessage("020200", tmpMap);
                    } else if ((Integer) value == 4) {
                        sendMessage("020300", tmpMap);
                    } else {
                        sendMessage("020400", tmpMap);
                    }
                    break;
                case "shutdown/mode":
                    if (value.toString().contains("shutdownTime")) {
                        ZSONObject zsonObject = ZSONObject.stringToZSON(value.toString());
                        int shutdownTime = zsonObject.getInteger("shutdownTime");
                        int shutdownMode = zsonObject.getInteger("shutdown/mode");
                        tmpMap.put("shutdown/mode", shutdownMode);
                        sendMessage("050" + shutdownTime + "00", tmpMap);
                    } else {
                        tmpMap.put("shutdown/mode", 0);
                        sendMessage("050000", tmpMap);
                    }
                    break;
                default:
                    break;
            }
            return dataMap;
        }
    };

    NetworkDeviceDataHandler(String deviceId, DeviceDataCallback deviceDataCallback) {
        super(deviceId, deviceDataCallback);
        dataModel = solarDataModel;
        mainHandler = new EventHandler(EventRunner.getMainEventRunner());
        mainHandler.postTask(() -> initProfileData(0, "", dataModel.recoverData()));
    }

    public static String getSessionId() {
        return sessionId;
    }

    public static void setSessionId(String id) {
        sessionId = id;
    }

    private void sendMessage(String message, HashMap<String, Object> tmpMap) {
        CommonInfo commonInfo = new CommonInfo();
        commonInfo.setSessionId(sessionId);
        HiLog.error(LABEL_LOG, "sessionId " + sessionId);
        NetConfigApi.getInstance().sendMessage(commonInfo, message, (code, controlMessage, str) -> {
            if (code == 0) {
                HiLog.info(LABEL_LOG, "send message success " + message);
                dataMap.putAll(tmpMap);
                mainHandler.postTask(() -> onDeviceDataChange(dataMap));
            } else {
                HiLog.error(LABEL_LOG, "send message fail code = " +
                        code + " control message = " + controlMessage);
            }
        });
    }

    @Override
    public void modifyDeviceProperty(String key, Object value) {
        dataModel.modifyProperty(key, value);
    }

    @Override
    void getData() {
        onDeviceDataChange(dataMap);
    }

    private interface DeviceDataModel {
        Map<String, Object> recoverData();

        Map<String, Object> modifyProperty(String key, Object value);
    }
}
